package com.mingmen.supplier.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mingmen.supplier.R;
import com.mingmen.supplier.model.CangKuModel;

import java.util.List;

/**
 * Contact联系人适配器
 *
 * @author nanchen
 * @fileName WaveSideBarView
 * @packageName com.nanchen.wavesidebarview
 * @date 2016/12/27  15:33
 * @github https://github.com/nanchen2251
 */

public class CangKuAdapter extends RecyclerView.Adapter<CangKuAdapter.ContactsViewHolder> {

    private List<CangKuModel.DataBean> dataBeanList;
    private static final String TAG = "ContactsAdapter";
    private OnitemClick onitemClick;   //定义点击事件接口
    Context context;

    public CangKuAdapter(Context context, List<CangKuModel.DataBean> contacts) {
        this.context = context;
        this.dataBeanList = contacts;
    }

    public void setDataBeanList(List<CangKuModel.DataBean> contacts) {
        this.dataBeanList = contacts;
        notifyDataSetChanged();
    }

    //定义设置点击事件监听的方法
    public void setOnitemClickLintener(OnitemClick onitemClick) {
        this.onitemClick = onitemClick;
    }

    @Override
    public ContactsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.layaout_item_qcangku, null);
        return new ContactsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ContactsViewHolder holder, int position) {
        CangKuModel.DataBean contact = dataBeanList.get(position);
        if (onitemClick != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //在TextView的地方进行监听点击事件，并且实现接口
                    onitemClick.onItemClick(position);
                }
            });
        }

        holder.text_location.setText(contact.getAddress());
        holder.text_cangkuname.setText(contact.getWarehouse());
    }


    @Override
    public int getItemCount() {
        return dataBeanList.size();
    }

    class ContactsViewHolder extends RecyclerView.ViewHolder {


        TextView text_location;
        TextView text_cangkuname;

        ContactsViewHolder(View itemView) {
            super(itemView);

            text_location = (TextView) itemView.findViewById(R.id.text_location);
            text_cangkuname = itemView.findViewById(R.id.text_cangkuname);
        }
    }

    //定义一个点击事件的接口
    public interface OnitemClick {
        void onItemClick(int position);
    }
}
